# base_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import logging

logger = logging.getLogger(__name__)

class BasePage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)
    
    def find_element(self, by, value):
        """封装查找元素方法"""
        logger.debug(f"查找元素: {by}={value}")
        return self.wait.until(EC.presence_of_element_located((by, value)))
    
    def click(self, by, value):
        """封装点击方法"""
        logger.debug(f"点击元素: {by}={value}")
        element = self.find_element(by, value)
        element.click()
    
    def send_keys(self, by, value, text):
        """封装输入文本方法"""
        logger.debug(f"在元素 {by}={value} 输入文本: {text}")
        element = self.find_element(by, value)
        element.clear()
        element.send_keys(text)
    
    def get_text(self, by, value):
        """封装获取文本方法"""
        logger.debug(f"获取元素文本: {by}={value}")
        element = self.find_element(by, value)
        return element.text
    
    def navigate_to(self, url):
        """封装导航到指定URL方法"""
        logger.info(f"导航到URL: {url}")
        self.driver.get(url)

    def element_is_displayed(self, by, value):
        """判断元素是否可见"""
        return self.find_element(by, value).is_displayed()